package unittest;

import java.io.File;
import java.util.Date;
import java.util.List;

import junit.framework.TestCase;
import project5.Corpus;
import project5.Document;
import project5.ErrorUtility;
import project5.IndexReader;
import project5.IndexWriter;
import project5.ProjectException;

public class IndexReaderTest extends TestCase
{
	private Corpus _corpus;
	private static final String STOP_WORDS_PATH = "C:/stopWords.txt";
	private static final String OUTPUT_FILE = "DTI.out";
	
	public void setUp()
	{
		Document doc1 = new Document("C:/Test1.txt");
		doc1.addWord("cat");
		doc1.setSize(17);
		doc1.setLineCount(3);
		doc1.setWordCount(2);
		
		Document doc2 = new Document("C:/Test2.txt");
		doc2.addWord("dog");
		doc2.setSize(13);
		doc2.setLineCount(22);
		doc2.setWordCount(9);
		
		Document doc3 = new Document("C:/Test3.txt");
		doc3.addWord("mouse");
		doc3.setSize(23);
		doc3.setLineCount(33);
		doc3.setWordCount(60);
		
		_corpus = new Corpus();
		List<Document> _documents = _corpus.getDocuments();
		_documents.add(doc1);
		_documents.add(doc2);
		_documents.add(doc3);
		
		_corpus.setLineSeparator("\r\n");
		_corpus.setStopWordsFilepath(STOP_WORDS_PATH);
		_corpus.setCreationDate(new Date());
		
		IndexWriter writer = new IndexWriter();
		try
		{
			writer.write(_corpus);
		}
		catch (ProjectException pe)
		{
			pe.printStackTrace();
			ErrorUtility.handleError(pe);
		}
		
		_corpus.setIndexFileSize((new File(OUTPUT_FILE)).length());
		
	}
	
	public void tearDown()
	{
		File f = new File("DTI.out");
		if (f.exists())
		{
			f.delete();
		}
	}
	
	public void testRead()
	{
		try
		{
			IndexReader reader = new IndexReader();
			Corpus corpus = reader.read();
			corpus.setIndexFileSize((new File(OUTPUT_FILE)).length());
		
			//Do comparisons here.
			assertEquals(_corpus.getBreakCharacters(), corpus.getBreakCharacters());
			assertEquals(_corpus.getStopWordsFilepath(), corpus.getStopWordsFilepath());
			assertEquals(_corpus.getIndexSize(), corpus.getIndexSize());
			assertEquals(_corpus.getIndexFileSize(), corpus.getIndexFileSize());
			assertEquals(_corpus.getCreationDate(), corpus.getCreationDate());
			assertEquals(_corpus.getLineSeparator(), corpus.getLineSeparator());
		}
		catch (Exception e)
		{
			e.printStackTrace();
			assertTrue(false);
		}
	}
}
